package com.example.malltest.client.controller;


import com.example.malltest.client.entity.Client;
import com.example.malltest.client.entity.ClientDTO;
import com.example.malltest.client.entity.ClientDTO1;
import com.example.malltest.client.entity.ClientPwd;
import com.example.malltest.client.service.ClientService;
import com.example.malltest.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户
 */
@RestController
@RequestMapping("client")
public class ClientController {

    @Autowired
    private ClientService clientService;


    /**
     * 登录
     * @param clientPwd
     * @return
     */
    @PostMapping("login")
    public R<Client> login(@RequestBody @Validated ClientPwd clientPwd){
        Client client = clientService.login(clientPwd);
        return R.ok(client);
    }
    /**
     *客户列表
     * @return
     */
    @GetMapping("list")
    public R<List<Client>> getList(){
        List<Client> clients = clientService.getList();
        return R.ok(clients);
    }

    /**
     * 增加客户
     * @param clientDTO
     * @return
     */
    @PostMapping("add")
    public R<Void> addClient(@RequestBody @Validated ClientDTO clientDTO){
        clientService.add(clientDTO);
        return R.ok();
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> delete(@PathVariable Integer id){
        clientService.delete(id);
        return R.ok();
    }

    /**
     * 编辑客户
     * @param id
     * @param clientDTO1
     * @return
     */
    @PutMapping("edit/{id}")
    public R<Void> edit(@PathVariable Integer id, @RequestBody @Validated ClientDTO1 clientDTO1){
        clientService.edit(id,clientDTO1);
        return R.ok();
    }

    /**
     * 更改状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("status/{id}")
    public R<Void> status(@PathVariable Integer id,@RequestParam String status){
        clientService.status(id,status);
        return R.ok();
    }

    /**
     * 获取信息
     * @param name
     * @return
     */
    @GetMapping("show")
    public R<Client> show(@RequestParam String name){
        Client client = clientService.getClient(name);
        return R.ok(client);
    }

    /**
     * 更改个人信息
     * @param name
     * @param client
     * @return
     */
    @PutMapping("change")
    public R<Void> change(@RequestParam String name,@RequestBody @Validated Client client){
        clientService.change(name,client);
        return R.ok();
    }

    /**
     * 修改密码
     * @param name
     * @param OldPassword
     * @param NewPassword
     * @return
     */
    @PutMapping("updatePwd")
    public R<Void> updatePwd(@RequestParam String name,@RequestParam String OldPassword,@RequestParam String NewPassword){
        clientService.updatePwd(name,OldPassword,NewPassword);
        return R.ok();
    }
}
